package com.jdk.learn.juc2.thread;

/**
 * 类功能描述:
 *
 * @author bgt
 * @version V1.0
 * @since 2019-01-31 15:25
 */
public class ThreadDeadLock {
    private static Object lockA = new Object();
    private static Object lockB = new Object();

    /**

     "ricky-B" #12 prio=5 os_prio=0 tid=0x000000001e3b7000 nid=0x27fc waiting for monitor entry [0x000000001ed2f000]
     java.lang.Thread.State: BLOCKED (on object monitor)
     at com.jdk.learn.juc2.thread.ThreadDeadLock.lambda$main$1(ThreadDeadLock.java:39)
     - waiting to lock <0x000000076b70da08> (a java.lang.Object)
     - locked <0x000000076b70da18> (a java.lang.Object)
     at com.jdk.learn.juc2.thread.ThreadDeadLock$$Lambda$2/109961541.run(Unknown Source)
     at java.lang.Thread.run(Thread.java:745)

     Locked ownable synchronizers:
     - None

     "ricky-A" #11 prio=5 os_prio=0 tid=0x000000001e3b4000 nid=0x348c waiting for monitor entry [0x000000001ec2f000]
     java.lang.Thread.State: BLOCKED (on object monitor)
     at com.jdk.learn.juc2.thread.ThreadDeadLock.lambda$main$0(ThreadDeadLock.java:25)
     - waiting to lock <0x000000076b70da18> (a java.lang.Object)
     - locked <0x000000076b70da08> (a java.lang.Object)
     at com.jdk.learn.juc2.thread.ThreadDeadLock$$Lambda$1/787604730.run(Unknown Source)
     at java.lang.Thread.run(Thread.java:745)

     Locked ownable synchronizers:
     - None


     */
    public static void main(String[] args) {
        Thread A = new Thread(() -> {
            synchronized (lockA) {
                try {
                    System.out.println("A:getA");
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("A:waitB");
                synchronized (lockB) {
                    System.out.println("A:getB");
                }
            }
        },"ricky-A");
        Thread B = new Thread(() -> {
            synchronized (lockB) {
                try {
                    System.out.println("B:getB");
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("B:waitA");
                synchronized (lockA) {
                    System.out.println("B:getA");
                }
            }
        },"ricky-B");

        A.start();
        B.start();

        // A:getA
        //B:getB
        //A:waitB
        //B:waitA
    }
}
